package com.example.jpa1.dao.impl;

import com.example.jpa1.dao.UserDao;
import com.example.jpa1.pojo.User;
import org.hibernate.Session;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

@Repository
public class UserDaoImpl  implements UserDao {

    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public User save(User user) {
//        entityManager.persist(user);
        Session session = entityManager.unwrap(Session.class);
        session.save(user);
        return null;
    }

    @Override
    public User edit(User user) {
        return null;
    }

    @Override
    public List<User> list() {
        String hsq = " from User";
        TypedQuery<User> query = entityManager.createQuery(hsq, User.class);
        List<User> resultList = query.getResultList();
        return resultList;
    }
}
